package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.bo.BrandBo;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketBrand;
import com.cskaoyan.app.bean.po.MarketBrandExample;
import com.cskaoyan.app.mapper.MarketBrandMapper;
import com.cskaoyan.app.service.AdminBrandService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 廖平安
 * @CreateTime: 2022-12-29  13:22
 * @Description: TODO品牌制造商 增 删 查 改
 * @Version: 1.0
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {
    @Autowired
    MarketBrandMapper brandMapper;

    /**
     * @description:展示全部信息
     * @author: 廖平安
     * @date: 2022/12/29 14:51
     * @param: [info, id, name]
     * @return: com.cskaoyan.app.bean.common.CommonData<com.cskaoyan.app.bean.po.MarketBrand>
     **/
    @Override
    public CommonData<MarketBrand> list(BasePageInfo info, Integer id, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketBrandExample example = new MarketBrandExample();
        example.setOrderByClause(StringsUtil.getOrderClause(info.getSort(), info.getOrder()));
        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringsUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = brandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        return CommonData.data(pageInfo);
    }

    /**
     * @description: 新增
     * @author: 廖平安
     * @date: 2022/12/29 16:10
     * @param: [brand]
     * @return: com.cskaoyan.app.bean.po.MarketBrand
     **/
    @Override
    public MarketBrand create(MarketBrand brand) {
        int i = brandMapper.insertSelective(brand);
        Integer id = brand.getId();
        MarketBrand marketBrand = brandMapper.selectByPrimaryKey(id);
        return marketBrand;
    }

    /**
     * @description:修改
     * @author: 廖平安
     * @date: 2022/12/29 16:10
     * @param: [brand]
     * @return: com.cskaoyan.app.bean.po.MarketBrand
     **/
    @Override
    public MarketBrand update(MarketBrand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
        Integer id = brand.getId();
        MarketBrand marketBrand = brandMapper.selectByPrimaryKey(id);
        return marketBrand;
    }

    /**
     * @description: 逻辑删除
     * @author: 廖平安
     * @date: 2022/12/29 16:11
     * @param: [brand]
     * @return: void
     **/
    @Override
    public void delete(MarketBrand brand) {
        brand.setDeleted(true);
        brandMapper.updateByPrimaryKeySelective(brand);
    }
}
